/*
 ============================================================================
 Name		: allegrosbAppUi.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : CallegrosbAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>

#include <allegrosb_0xE6249DB5.rsg>

#include "allegrosb.hrh"
#include "allegrosb.pan"
#include "allegrosbApplication.h"
#include "allegrosbAppUi.h"
#include "allegrosbAppView.h"

_LIT( KFileName, "C:\\private\\E6249DB5\\allegrosb.txt" );
_LIT( KText, "Hello World!");

#include "allegro.h"
#include "dumy.h"
#include "aintern.h"

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CallegrosbAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CallegrosbAppUi::ConstructL()
	{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	// Create view object
	iAppView = CallegrosbAppView::NewL(ClientRect());

	// Create a file to write the text to
	TInt err = CCoeEnv::Static()->FsSession().MkDirAll(KFileName);
	if ((KErrNone != err) && (KErrAlreadyExists != err))
		{
		return;
		}

	RFile file;
	err = file.Replace(CCoeEnv::Static()->FsSession(), KFileName, EFileWrite);
	CleanupClosePushL(file);
	if (KErrNone != err)
		{
		CleanupStack::PopAndDestroy(1); // file
		return;
		}

	RFileWriteStream outputFileStream(file);
	CleanupClosePushL(outputFileStream);
	outputFileStream << KText;

	CleanupStack::PopAndDestroy(2); // outputFileStream, file
	isStartGame = 0;
	}
// -----------------------------------------------------------------------------
// CallegrosbAppUi::CallegrosbAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CallegrosbAppUi::CallegrosbAppUi()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CallegrosbAppUi::~CallegrosbAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CallegrosbAppUi::~CallegrosbAppUi()
	{
	if (iAppView)
		{
		delete iAppView;
		iAppView = NULL;
		}

	}

int enigma_scrambler(int key)
    {
       /* Add one to both the scancode and ascii values. */
       return (((key >> 8) + 1) << 8) | ((key & 0xff) + 1);
    }
END_OF_FUNCTION(enigma_scrambler)


int test()
	{
	
	   /* you should always do this at the start of Allegro programs */
	set_color_depth(16);
	   if (allegro_init() != 0)
	      return 1;

	   /* set up the keyboard handler */
	   
	   install_keyboard(); 
	   install_timer();
	   keyboard_callback = enigma_scrambler;

	   /* set a graphics mode sized 320x200 */
	   if (set_gfx_mode(GFX_AUTODETECT, 240, 320, 240, 320) != 0) {
	      if (set_gfx_mode(GFX_SAFE, 320, 240, 320, 240) != 0) {
		 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		 allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
		 return 1;
	      }
	   }
	   
	   /* set the color palette */
	   set_palette(desktop_palette);

	   /* clear the screen to white */
	   clear_to_color(screen, makecol(255, 255, 255));

//	   /* you don't need to do this, but on some platforms (eg. Windows) things
//	    * will be drawn more quickly if you always acquire the screen before
//	    * trying to draw onto it.
//	    */
	   
	   acquire_screen();
//
//	   /* write some text to the screen with black letters and transparent background */
	   textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);
//
//	   /* you must always release bitmaps before calling any input functions */
	   release_screen();
	   direct_render();
	   /* wait for a key press */
//	   readkey();
	   
	   return 0;
	}

void CallegrosbAppUi::draw_to_native(unsigned char* data, int w, int h)
	{
	 this->iAppView->draw_to_native(data, w, h);
	}

TKeyResponse CallegrosbAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	TKeyResponse res;
	if(isStartGame)
		{
		switch(aType)
			{
			case EEventKeyDown:
				_handle_key_press(aKeyEvent.iCode, aKeyEvent.iScanCode);
				break;
			case EEventKeyUp:
				_handle_key_release(aKeyEvent.iScanCode);
				break;
			}
	
		}
	return res;
	}

// -----------------------------------------------------------------------------
// CallegrosbAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CallegrosbAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
		case EEikCmdExit:
		case EAknSoftkeyExit:
			Exit();
			break;
			
		case ECommand1:
			{
			isStartGame = 1;
			
			set_color_depth(16);
			keyboard_callback = enigma_scrambler;
			main();
//			test();
			  //direct_render();
//			// Load a string from the resource file and display it
//			HBufC* textResource = StringLoader::LoadLC(R_COMMAND1_TEXT);
//			CAknInformationNote* informationNote;
//
//			informationNote = new (ELeave) CAknInformationNote;
//
//			// Show the information Note with
//			// textResource loaded with StringLoader.
//			informationNote->ExecuteLD(*textResource);
//
//			// Pop HBuf from CleanUpStack and Destroy it.
//			CleanupStack::PopAndDestroy(textResource);
			}
			break;
		case ECommand2:
			{
			RFile rFile;

			//Open file where the stream text is
			User::LeaveIfError(rFile.Open(CCoeEnv::Static()->FsSession(),
					KFileName, EFileStreamText));//EFileShareReadersOnly));// EFileStreamText));
			CleanupClosePushL(rFile);

			// copy stream from file to RFileStream object
			RFileReadStream inputFileStream(rFile);
			CleanupClosePushL(inputFileStream);

			// HBufC descriptor is created from the RFileStream object.
			HBufC* fileData = HBufC::NewLC(inputFileStream, 32);

			CAknInformationNote* informationNote;

			informationNote = new (ELeave) CAknInformationNote;
			// Show the information Note
			informationNote->ExecuteLD(*fileData);

			// Pop loaded resources from the cleanup stack
			CleanupStack::PopAndDestroy(3); // filedata, inputFileStream, rFile
			}
			break;
		case EHelp:
			{

			}
			break;
		case EAbout:
			{

			CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog();
			dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
			HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
			dlg->QueryHeading()->SetTextL(*title);
			CleanupStack::PopAndDestroy(); //title
			HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
			dlg->SetMessageTextL(*msg);
			CleanupStack::PopAndDestroy(); //msg
			dlg->RunLD();
			}
			break;
		default:
			Panic(EallegrosbUi);
			break;
		}
	}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CallegrosbAppUi::HandleStatusPaneSizeChange()
	{
	iAppView->SetRect(ClientRect());
	}




// End of File
